// 初始化、自动增长地图
import { ref, nextTick, inject } from "vue";
import { ECharts } from "echarts/core";
// 2. 拿到图表实例
export const centerMapRef = ref<{ chart: ECharts }>();
const MAP_LIGHT_DELAY = 1000 * 10;
const MAP_ADD_IMG_TIME = 1000 * 1;
const WIDTH = 1.8;
const SPEED = 1;
const COLOR = "rgb(0,180, 255)";
// 飞线信息接口
interface FlyLineInfo {
    id?: number;
    cityName: string;
    from: [number, number, number];
    to: [number, number, number][];
    color: string;
    width: number;
    speed: number;
    radius: number;
}
export type OnLightDone = (id: number, cityName: string) => void;
export function initMock(onLightDone?: OnLightDone) {
    // 飞线数组
    const flyLines: FlyLineInfo[] = [
        { id: 1, cityName: "厦门市", from: [0, 0, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 2, cityName: "南平市", from: [0, 50, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 3, cityName: "宁德市", from: [22, 60, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 4, cityName: "三明市", from: [-22, 30, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 5, cityName: "福州市", from: [20, 50, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 6, cityName: "龙岩市", from: [-30, 5, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 7, cityName: "莆田市", from: [13, 19, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 8, cityName: "泉州市", from: [-5, 5, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
        { id: 9, cityName: "漳州市", from: [-16, -10, 0], to: [], color: COLOR, width: WIDTH, radius: WIDTH, speed: SPEED },
    ];
    let aim = [
        { id: 1, name: "订单信息", to: [-180, 65, 20] },
        { id: 2, name: "用户统计", to: [-240, -60, 10] },
        { id: 3, name: "会员权益", to: [-390, -370, 30] },
        { id: 4, name: "实时订单量", to: [30, 79, -100] },
        { id: 5, name: "实时贡献值", to: [50, 0, -120] },
        { id: 6, name: "实时权益值", to: [90, -100, -120] },
    ];
    let timer = null;
    // 高亮/取消高亮
    function highlightCities(centerMapRef: any, highlight: boolean, cityName: string) {
        if (!centerMapRef.value) return;
        centerMapRef.value.chart.dispatchAction({
            type: highlight ? "highlight" : "downplay",
            seriesIndex: 0, // 你的地图系列索引
            name: cityName, // 区域名称，必须和 geoJSON 里的 name 完全一致
        });
    }
    // 开始mock数据
    function startMock() {
        // timer = setTimeout(() => {
        //     const option = getRandomArrayItem(flyLines); // 得到随机数据
        //     const toItem = getRandomArrayItem(aim); // 目标地点
        //     highlightCities(centerMapRef, true, option.cityName);
        //     setTimeout(() => {
        //         onLightDone?.(option.id, option.cityName); // 抛事件
        //         highlightCities(centerMapRef, false, option.cityName);
        //     }, MAP_ADD_IMG_TIME);
        // }, MAP_LIGHT_DELAY);
    }
    startMock();
}
/** 随机返回数组里的一个元素 */
export function getRandomArrayItem(array: [...any]): any {
    const idx = Math.floor(Math.random() * array.length);
    return array[idx];
}
